package com.ruoyi.column.service.impl;

import com.ruoyi.DynamicClassInfo;
import com.ruoyi.column.entity.Column;
import com.ruoyi.column.service.ColumnService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.util.DynamicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 动态列表对象 服务实现类
 * </p>
 *
 * @author XiaShiLong
 * @since 2021-03-22
 */
@Service
public class ColumnServiceImpl implements ColumnService {

    /**
     * 缓存
     */
    private final RedisCache redisCache;


    @Autowired
    public ColumnServiceImpl(RedisCache redisCache) {
        this.redisCache = redisCache;
    }


    /**
     * 获取前端列表对象集合
     *
     * @param classPath 类路径
     * @param menuId    菜单id
     * @return List
     */
    public List<Column> getTable(String classPath, Long menuId) {
        DynamicClassInfo dynamicClassInfo = DynamicUtil.resolveClass(classPath, true);
        String key = redisKey(dynamicClassInfo.getClassPath());
        List<Column> cacheList = redisCache.getCacheList(key);
        if (cacheList.isEmpty() || dynamicClassInfo.getFieldNumber() != cacheList.size()) {
            cacheList.clear();
            cacheList.addAll(
                    dynamicClassInfo.getFieldInfoList()
                            .stream()
                            .map(
                                    f -> Column.defaultBuilder(f, dynamicClassInfo.getSimpleName(), SecurityUtils.getUserId(), menuId, dynamicClassInfo.getClassPath())
                            )
                            .collect(Collectors.toList())
            );
            saveCache(cacheList, key);
        }
        return cacheList;
    }

    /**
     * 自定义列表
     *
     * @param columnList 列集合
     * @return List
     */
    @Override
    public List<Column> customizeTable(List<Column> columnList) {
        if (!columnList.isEmpty()) {
            String key = redisKey(columnList.get(0).getClassPath());
            List<Column> cacheList = redisCache.getCacheList(key);
            if (!cacheList.isEmpty()) {
                redisCache.deleteObject(key);
                cacheList.sort(Comparator.comparing(Column::getSort));
            }
            redisCache.setCacheList(key, columnList);
        }
        return columnList;
    }

    /**
     * 还原默认设置
     *
     * @param classPath 类路径
     */
    @Override
    public void restoreDefault(String classPath) {
        redisCache.deleteObject(redisKey(classPath));
    }

    /**
     * 获取缓存key
     *
     * @param classPath 类路径
     * @return key
     */
    private static String redisKey(String classPath) {
        return "dynamic_list:" + classPath + ":" + SecurityUtils.getUserId();
    }

    /**
     * 更新缓存
     *
     * @param cacheList 缓存列集合
     * @param key       缓存key
     */
    private synchronized List<Column> saveCache(List<Column> cacheList, String key) {
        redisCache.deleteObject(key);
        redisCache.setCacheList(key, cacheList);
        return cacheList;
    }

}
